const app = getApp()

Page({
  data: {
    globalData: app.globalData,
    isClose: false, //true
    oID: '',
    //订单列表
    orderList: [],
    orderInfo: [],
    pageNo: '0',
    pageSize: '',
    count: '',
    //存储计时器
    setInter: '',
  },
  // 页面默认事件
  onLoad: function (options) {
    var that = this;

    //验证登录，如果当前全局没有值，读取setStrong，如果还没有，就去登录页面
    // app.userLogin();

    //获取用户数据缓存
    wx.getStorage({
      key: 'users',
      success: function (e) {
        app.globalData.userInfo = e.data;
        that.setData({
          userInfo: e.data,
          globalData: app.globalData,
        })
      }
    })
    that.getOrderList('init');
    that.data.setInter = setInterval(
      function () {
        that.getOrderList('init');
      }, 10000);
  },
  onUnload: function () {
    var that = this;
    clearInterval(that.data.setInter)
  },
  ClearMapGPS: function() {
    //清除信息
    var that = this
    app.globalData.map.endAddress = '目的地';
    app.globalData.map.endLatitude = '';
    app.globalData.map.endLongitude = '';
    that.setData({
      markers: {},
      globalMapData: app.globalData.map,
      isGoBtn: false,
      isGotoBackBtn: false
    });
  },
  loadOrderInfo: function(e) {
    app.request({
      url: "/reserveCarForm",
      data: {
        id: e.currentTarget.dataset.name
      },
      success: (res) => {
        this.setData({
          orderInfo: res.data.data,
          isClose: true
        })
        console.log('orderInfo---', this.data.orderInfo)
      }
    })
  },
  // 阻止冒泡
  catchBind: function() {
    //不干嘛就阻止冒泡
    console.log('事件冒泡阻止');
  },
  //拨打电话
  calling: function(e) {
    var callPhone = e.target.dataset.num;
    console.log('拨打号码:', callPhone);
    wx.makePhoneCall({
      phoneNumber: callPhone,
      success: function() {
        console.log("拨打电话成功！")
      },
      fail: function() {
        console.log("拨打电话失败！")
      }
    })
  },
  //点击关闭详情
  clickClose: function(e) {
    this.setData({
      isClose: false
    });
  },
  // tabMenu: function(e) {
  //   //顶部菜单点击
  //   var that = this;
  //   app.globalData.index.curIndex = e.target.dataset.id;
  //   console.log(app.globalData.index.curIndex)
  //   that.setData({
  //     globalData: app.globalData,
  //   });
  //   wx.navigateTo({
  //     url: '../index/index?cID=' + e.target.dataset.id
  //   })
  // },
  // 取消订单
  cancelOrder: function(e) {
    var that = this;
    var clearID = e.currentTarget.dataset.name
    wx.showModal({
      title: '系统提示',
      content: '点击确定取消订单\r\n（取消两次订单后本日内将无法使用该小程序对应功能）',
      mask: true,
      success(res) {
        if (res.confirm) {
          app.request({
            url: "/reserveCarCancel",
            data: {
              id: clearID,
              orderStatus: '5'
            },
            success: (res) => {
              wx.showToast({
                title: '已成功取消订单',
                icon: 'success',
                mask: true,
                duration: 2000
              })
              //操作成功，重新加载数据
              // this.loadOrderInfo();
              that.getOrderList('init');
              that.clickClose();
            }
          })
          //this.getOrderList();
          //this.clickClose();
        } else if (res.cancel) {
          console.log('取消操作')
        }
      }
    })
  },
  // 完成订单
  // completeOrder: function(e) {
  //   var that = this;
  //   var completeID = e.currentTarget.dataset.name
  //   wx.showModal({
  //     title: '系统提示',
  //     content:'点击确定完成订单',
  //     success(res) {
  //       if (res.confirm) {
  //         app.request({
  //           url: "/reserveCarComplete",
  //           data: {
  //             id: completeID,
  //             orderStatus: '4'
  //           },
  //           success: (res) => {
  //             wx.showToast({
  //               title: '已成功完成订单',
  //               icon: 'success',
  //               duration: 1500
  //             })
  //             //操作成功，重新加载数据
  //             that.getOrderList('init');
  //             that.clickClose();
  //           }
  //         })
  //       } else if (res.cancel) {
  //         console.log('取消操作')
  //       }
  //     }
  //   })
  // },
  // 获取订单列表
  getOrderList: function(type) {
    console.log('getOrderList', type);
    var that = this;
    let pageNo = that.data.pageNo + 1;
    // 刷新数据
    if (type == 'init') {
      pageNo = 1;
    }
    app.request({
      url: "/reserveCarList",
      data: {
        // pageNo: pageNo,
        openId: app.openId,
        name: app.globalData.userInfo.name,
        phone: app.globalData.userInfo.phone,
        storeId: app.globalData.userInfo.storeId,
      },
      success: (res) => {
        console.log('orderList', res.data.data)
        var orderList = res.data.data;

        if (type == 'init') {
          that.data.orderList = orderList;
        } else {
          for (var i = 0; i < orderList.length; i++) {
            that.data.orderList.push(orderList[i])
          }
        }

        that.setData({
          // count: orderList.count,
          // pageSize: orderList.pageSize,
          // pageNo: that.data.pageNo + 1,
          orderList: that.data.orderList,
          hiddenloading: true
        })

      }
    })
  },
  // 下拉刷新
  onPullDownRefresh() {
    wx.stopPullDownRefresh();
    this.getOrderList('init')
  },
  // 上拉加载下一页
  // onReachBottom: function() {
  //   console.log('加载更多')
  //   // this.setData({
  //   //   hiddenloading: false
  //   // })
  //   this.getOrderList()
  // },
})